package form.core
{
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import form.ui.ApplicationContainer;
	import form.ui.core.FMSprite;
	
	public class TooltipManager
	{
		public static const MIN_WIDTH:uint = 30;
		public static const MIN_HEIGHT:uint = 18;
		public static const FILL_COLOR:uint = 0xFFFFCC;
		
		public static const OFFSET_X:uint = 10;
		public static const OFFSET_Y:uint = 2;
		
		public static const CLOSE:uint = 3000;
		public static const DELAY:uint = 1000;
		
		public var paddingTop:Number = 0;
		public var paddingRight:Number = 5;
		public var paddingBottom:Number = 5;
		public var paddingLeft:Number = 5;
		
		private var application:ApplicationContainer = ApplicationContainer.application;
		
		private var toolTipTextfield:TextField;
		private var toolTipLayout:Sprite;
		private var timer:Timer;
		
		public function showToolTip(target:FMSprite, x:Number, y:Number):void
		{
			if(!target || !target.parent || !target.toolTip || target.toolTip == "") return;
			
			var sw:Number = application.stage.stageWidth;
			var sh:Number = application.stage.stageHeight;
			
			removeToolTip();
			
			toolTipTextfield.text = target.toolTip;
			toolTipTextfield.width = toolTipTextfield.textWidth > MIN_WIDTH ? toolTipTextfield.textWidth + 5 : MIN_WIDTH;
			toolTipTextfield.height = toolTipTextfield.textHeight > MIN_HEIGHT ? toolTipTextfield.textHeight + 5 : MIN_HEIGHT;
			
			var width:Number = toolTipTextfield.width + paddingLeft + paddingRight;
			var height:Number = toolTipTextfield.height + paddingTop + paddingBottom;
									
			toolTipLayout = new Sprite();
			toolTipLayout.graphics.beginFill(FILL_COLOR);
			toolTipLayout.graphics.drawRoundRect(0,0,width, height, 5,5);
			toolTipLayout.graphics.endFill();
			toolTipLayout.mouseEnabled = false;
			
			toolTipTextfield.x = paddingLeft;
			toolTipTextfield.y = height / 2 - toolTipTextfield.height / 2;
			
			toolTipLayout.addChild(toolTipTextfield);
			
			if(x < sw - toolTipLayout.width)
				toolTipLayout.x = x + OFFSET_X;
			else
				toolTipLayout.x = x - toolTipLayout.width - OFFSET_X;
			
			if(y < sh - toolTipLayout.height)
				toolTipLayout.y = y + OFFSET_Y;
			else
				toolTipLayout.y = y - toolTipLayout.height - OFFSET_Y;
			
			application.addOverlay(toolTipLayout);
			
			timer.stop();
			timer.reset();
			timer.start();
		}
		
		public function removeToolTip(e:TimerEvent = null):void
		{
			if(toolTipLayout)
			{
				if(toolTipTextfield && toolTipLayout.contains(toolTipTextfield)) 
					toolTipLayout.removeChild(toolTipTextfield);
				application.removeOverlay(toolTipLayout);
			}
		}
		
		// Class Instance
		private static var instance:TooltipManager;

		public function TooltipManager(enforcer:SingletonEnforcer)
		{
			toolTipTextfield = new TextField();
			toolTipTextfield.width = MIN_WIDTH;
			toolTipTextfield.height = MIN_HEIGHT;
			toolTipTextfield.selectable = false;
			toolTipTextfield.autoSize = TextFieldAutoSize.LEFT;
			toolTipTextfield.mouseEnabled = false;
			toolTipTextfield.defaultTextFormat = new TextFormat(null, 12, 0x333333);
			
			timer = new Timer(CLOSE, 1);
			timer.addEventListener(TimerEvent.TIMER, removeToolTip, false, 0, true);
		}

		public static function getInstance():TooltipManager
		{
			if(TooltipManager.instance == null) 
			{
				TooltipManager.instance = new TooltipManager(new SingletonEnforcer());
			}
			return TooltipManager.instance;
		}
	}
}
class SingletonEnforcer {}